package com.xh3140.xwidget.dialog.view;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

import com.xh3140.xwidget.dialog.params.HeaderParams;

import java.util.Objects;

/**
 * 对话框首部视图
 */
@SuppressWarnings("unused")
public class HeaderView extends LinearLayoutCompat {
  private TitleView mTitleView;
  private SubtitleView mSubtitleView;

  public HeaderView(@NonNull Context context) {
    super(context);
    setOrientation(VERTICAL);
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  }

  public HeaderView configView(@NonNull final HeaderParams params) {
    removeAllViews();
    if (params.title.text != null) {
      mTitleView = new TitleView(getContext()).configView(params.title);
      addView(mTitleView);
    }
    if (params.subtitle.text != null) {
      mSubtitleView = new SubtitleView(getContext()).configView(params.subtitle);
      addView(mSubtitleView);
    }
    setVisibility(isGoneTitleView() && isGoneSubtitleView() ? GONE : VISIBLE);
    return this;
  }

  @Override
  public void removeAllViews() {
    mTitleView = null;
    mSubtitleView = null;
    if (getChildCount() > 0) {
      super.removeAllViews();
    }
  }

  @Nullable
  public TitleView getTitleView() {
    return mTitleView;
  }

  @NonNull
  public TitleView requireTitleView() {
    return Objects.requireNonNull(mTitleView);
  }

  @Nullable
  public SubtitleView getSubtitleView() {
    return mSubtitleView;
  }

  @NonNull
  public SubtitleView requireSubtitleView() {
    return Objects.requireNonNull(mSubtitleView);
  }

  public boolean isGoneTitleView() {
    return mTitleView == null || mTitleView.getVisibility() == GONE;
  }

  public boolean isGoneSubtitleView() {
    return mSubtitleView == null || mSubtitleView.getVisibility() == GONE;
  }
}
